package com.example.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wms.common.QueryPageParam;
import com.example.wms.common.Result;
import com.example.wms.entity.Sclass;
import com.example.wms.entity.Tutor;
import com.example.wms.service.ISclassService;
import com.example.wms.service.ITutorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2023-01-15
 */
@RestController
@RequestMapping("/sclass")
public class SclassController {

    @Autowired
    private ISclassService sclassService;


    @GetMapping("/list")
    public Result list(){
        return Result.success(sclassService.list());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Sclass sclass){
        System.out.println("user save"+sclass);
        return sclassService.save(sclass)?Result.success():Result.fail();
    }

    @PostMapping("/update")
    public Result saveOrMod(@RequestBody Sclass sclass){
        System.out.println("Sclass update"+sclass);
        return sclassService.saveOrUpdate(sclass)?Result.success():Result.fail();
    }

    @GetMapping("/del")
    public Result delete(Integer id){
        return sclassService.removeById(id)?Result.success():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String faculty = (String)param.get("faculty");
        String grade = (String)param.get("grade");
        Page<Sclass> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Sclass> lambdaQueryWrapper = new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(Sclass::getName,name);

        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Sclass::getName,name);
        }

        if(StringUtils.isNotBlank(faculty) && !"null".equals(faculty)){
            lambdaQueryWrapper.eq(Sclass::getFaculty,faculty);
        }
        if(StringUtils.isNotBlank(grade) && !"null".equals(grade)){
            lambdaQueryWrapper.eq(Sclass::getGrade,grade);
        }

        IPage result = sclassService.page(page,lambdaQueryWrapper);
        System.out.println("total=="+result.getTotal());
        return Result.success(result.getRecords(),result.getTotal());
    }
}
